import database.didactic.ActivityTypeDao;
import database.sporteli.ActionInformationDao;
import database.util.ArrayListToXML;
import model.didactic.ActivityType;
import model.sporteli.ActionInformation;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created with IntelliJ IDEA.
 * Author: Kostika Gorica
 * User: kostika
 * Date: 1/3/13
 * Time: 3:58 PM
 */
public class SporteliActionInformationServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestedResource = request.getParameter("date");
        PrintWriter out = response.getWriter();

        if(requestedResource==null || requestedResource.trim().length()<1){
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.sendError(HttpServletResponse.SC_BAD_REQUEST,"Kerkesa nuk eshte e sakte");
            return;
        }
        if (requestedResource.equalsIgnoreCase("today")){
            GregorianCalendar start = new GregorianCalendar();
            start.set(GregorianCalendar.HOUR, 0);
            Calendar end = Calendar.getInstance();
            end.set(GregorianCalendar.HOUR,23);
            ArrayList<ActionInformation> arrayList= ActionInformationDao.selectActionInformationBetwen(new Date(start.getTimeInMillis()),new Date(end.getTimeInMillis()));
            if (arrayList!= null && arrayList.size()>0){
                String xmlResult = ArrayListToXML.toActionInformationXML(arrayList);
                if (xmlResult!=null && xmlResult.trim().length()>0){
                    response.setStatus(HttpServletResponse.SC_OK);
                    out.println(xmlResult);
                } else {
                    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Ndodhi nje gabim ne server!");
                }
            } else {
                response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                response.sendError(HttpServletResponse.SC_NO_CONTENT,"Kerkesa nuk dha asnje rezultat!");
            }
        } else if (requestedResource.equalsIgnoreCase("dateRange") || requestedResource.equalsIgnoreCase("timeRange")){
              //date format "yyyy-MM-dd"    or "yyyy-MM-dd'T'HH:mm:ss"
            Date start = null;
            Date end = null;
            String startDateString=request.getParameter("start");
            String endDateString=request.getParameter("end");
            if ((startDateString==null) || (endDateString==null) || (startDateString.trim().length()<1) || endDateString.trim().length()<1){
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,"Kerkesa nuk eshte e sakte");
            }else {
                if (requestedResource.equalsIgnoreCase("dateRange")){
                    SimpleDateFormat dateRangeDF = new SimpleDateFormat("yyyy-MM-dd");
                    try{
                        start = dateRangeDF.parse(startDateString);
                        end = dateRangeDF.parse(startDateString);
                    } catch (ParseException e) {
                        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                        response.sendError(HttpServletResponse.SC_BAD_REQUEST,"Start/End e formatuar keq");
                        e.printStackTrace();
                        return;
                    }
                } else  if (requestedResource.equalsIgnoreCase("timeRange")){
                    SimpleDateFormat dateRangeDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                    try{
                        start = dateRangeDF.parse(startDateString);
                        end = dateRangeDF.parse(startDateString);
                    } catch (ParseException e) {
                        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                        response.sendError(HttpServletResponse.SC_BAD_REQUEST,"Start/End e formatuar keq");
                        e.printStackTrace();
                        return;
                    }
                }
                if (start== null || end == null){
                    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Ndodhi nje gabim ne server!");
                }
                ArrayList<ActionInformation> arrayList= ActionInformationDao.selectActionInformationBetwen(start,end);
                if (arrayList!= null && arrayList.size()>0){
                    String xmlResult = ArrayListToXML.toActionInformationXML(arrayList);
                    if (xmlResult!=null && xmlResult.trim().length()>0){
                        response.setStatus(HttpServletResponse.SC_OK);
                        out.println(xmlResult);
                    } else {
                        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Ndodhi nje gabim ne server!");
                    }
                } else {
                    response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                    response.sendError(HttpServletResponse.SC_NO_CONTENT,"Kerkesa nuk dha asnje rezultat!");
                }
            }
        }  else {
            response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
            response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED,"Resoursi i kerkuarn nuk eshte implementuar");
        }
    }
}
